package com.qianfeng.qlm.handforireader.fragment;

import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.qianfeng.qlm.handforireader.R;
import com.qianfeng.qlm.handforireader.adapter.ActiveListRecyclerAdapter;
import com.qianfeng.qlm.handforireader.model.ActiveBean;
import com.qianfeng.qlm.handforireader.model.ActiveBeanList;
import com.qianfeng.qlm.handforireader.util.Constant;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA
 * User:tadhg
 * Date:2015/3/23
 * Email:tadhg299@163.com
 */
public class ActiveFragment extends Fragment implements View.OnClickListener{

    private RequestQueue requestQueue;
    private ActiveListRecyclerAdapter adapter;
    private RecyclerView recyclerView;
    private RelativeLayout rlLoading;
    private ImageView imgLoading;
    private AnimationDrawable animationDrawable;

    public ActiveFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestQueue = Volley.newRequestQueue(getActivity());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_active, container, false);
        rlLoading = (RelativeLayout) view.findViewById(R.id.rl_loading);
        imgLoading = (ImageView) view.findViewById(R.id.active_loading_img);
        recyclerView = (RecyclerView) view.findViewById(R.id.recyler_view);
        LinearLayoutManager layout = new LinearLayoutManager(getActivity());
        layout.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layout);
        ArrayList<ActiveBean> data = new ArrayList<>();
        adapter = new ActiveListRecyclerAdapter(getActivity(), data,this);
        recyclerView.setAdapter(adapter);
        initRequest();

        return view;
    }

    private void initRequest(){
        JsonObjectRequest request = new JsonObjectRequest(
                Constant.URL_ACTIVE,
                null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        if (response != null){
                            animationDrawable.stop();
                            rlLoading.setVisibility(View.GONE);
                            ActiveBeanList beanList = new ActiveBeanList();
                            beanList.parseJson(response);
                            List<ActiveBean> activeBeans = beanList.getBeanList();
                            if (activeBeans != null){
                                adapter.setData(activeBeans);
                            }
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                }
        );

        animationDrawable = (AnimationDrawable) imgLoading.getDrawable();
        animationDrawable.start();
        requestQueue.add(request);
    }

    @Override
    public void onClick(View v) {
        int position = recyclerView.getChildPosition(v);


    }
}
